<!DOCTYPE html>
<html>
<head>
<title>新增治疗单据项目信息</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css" media="all">
<style type="text/css">
select {
	height: 38px;
	line-height: 1.3;
	line-height: 38px\9;
	border-width: 1px;
	border-style: solid;
	background-color: #fff;
	border-radius: 2px;
	width: 100%;
}

h1 {
	color: #c2c2c2;
	line-height: 25px;
	font-size: 16px;
	text-align: center;
	margin-top: 5px;
}

legend {
	font: 22px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
		sans-serif;
	width: auto;
	margin: 0px auto;
}

.row {
	margin-top: 10px;
}

.no_border {
	border: 0px;
}

#btn_search {
	margin-top: 8px;
	margin-left: -25px;
}
</style>
</head>

<body>
	<div id="div_fundBill" class="layui-form layui-container" style="">
		<fieldset style="border-width: 0px;margin-top:25px">
			<legend>
				<a>费用单据项目信息</a>
			</legend>
		</fieldset>
		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">费用项目: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2 ">
				<select id="projectNameSelect" name="projectName" lay-verify="required" lay-serach="" lay-filter="projectNameSelect">
                </select>
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">类型: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2">
				<input id="type" type="text" name="type" class="layui-input" readonly>
            </div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">费用金额: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2">
				<input type="text" name="cost" lay-verify="number" placeholder="￥" autocomplete="off" class="layui-input">
           </div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">其他备注: </span>
			</div>
			<div class="layui-col-xs7 layui-col-sm4 layui-col-md3">
				<textarea name="extraInfo" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
		</div>
		<input id="submit" style="display:none" type="button" lay-submit lay-filter="*">
	</div>
</body>

<script type="text/javascript" src="/Hospital/lib/jquery.min.js"></script>
<script type="text/javascript" src="/Hospital/lib/layui-v2.1.6/layui/layui.js"></script>
<script>
    var isSuccess=0;        //判断是否新增成功
    layui.use(['form','laydate'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var laydate = layui.laydate; //调用Layui插件里的时间组件
        form.render();   //layui form表单更新渲染
        
        initFrame();     //初始化窗口数据
        listen();        //监听事件
        
       	function initFrame() {  //初始化窗口
        	var operator = localStorage.getItem("importBill_operator");
       		if(operator == 1) { 	//添加
       			initAddFrame();
       		}
       		if(operator == 2) { 	//编辑
       			initAlterFrame();
       		}
       		
        }
      	
        //点击添加加载窗口
        function initAddFrame() {
        	$("input[name='cost']").val(0);
        	//初始化费用项目
        	$.ajax({
        		type : "post",
        		url : "/Hospital/projectController/getAllFundProject",
        		data : "",
        		success : function(response) {
        			if(response.code != 200) {
        				console.error(response);
        				return;
        			}
        			var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var projectId = result[i].projectId;
						var projectName = result[i].projectName;
						var type = result[i].type;
						var typeInt = result[i].typeInt;
						var name = result[i].name;
						$("#projectNameSelect").append(
								"<option value="+projectId+" type="+type+ " typeInt="+typeInt+">" + projectName + "</option>");
					}
					
					//初始化类型
					var type = $("#projectNameSelect option:selected").attr("type");
	                var typeInt = $("#projectNameSelect option:selected").attr("typeInt");
	                $("#type").val(type);
	                $("#type").attr("typeInt",typeInt);
					//重绘
					form.render();
        		}
        	});
        }
        
        //点击编辑时加载窗口
        function initAlterFrame() {
        	var data = JSON.parse(localStorage.getItem("editTableData"));
        	console.log("addFundBillProject.initAlterFrame.data");
        	console.log(data);
        	$("input[name='cost']").val(data.cost);
        	$.ajax({
        		type : "post",
        		url : "/Hospital/projectController/getAllFundProject",
        		data : "",
        		success : function(response) {
        			if(response.code != 200) {
        				console.error(response);
        				return;
        			}
        			var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var projectId = result[i].projectId;
						var projectName = result[i].projectName;
						var type = result[i].type;
						var typeInt = result[i].typeInt;
						var name = result[i].name;
						$("#projectNameSelect").append(
								"<option value="+projectId+" type="+type+ " typeInt="+typeInt+">" + projectName + "</option>");
					}
					$("#projectNameSelect").val(data.projectId);
					//初始化类型
	                $("#type").val(data.type);
	                $("#type").attr("typeInt",data.typeInt);
					//重绘
					form.render();
        		}
        	});
        }
        
        function resize(){
            var bodyHeight=parent.$('#iframeHeight').val()*0.7;
            var bodyWidth=parent.$('#iframeWidth').val()*0.96;
            $('body').css({'height': '300px','width':bodyWidth}); //写死了body高度
            /* var iconSize=$(document.body).width()*0.05;
            $('.icon').css({'font-size':iconSize}); */
        }
        
        function listen(){
            form.on('submit(*)',function () {   //提交监听
                var data=formData();     //获取表单数据
                localStorage.setItem("importBillIframeData",JSON.stringify(data));  //传值
                isSuccess=200;
                $("select[name='projectName']").val("");
                $("input[name='number']").val("");
                $("input[name='cost']").val("");
                $("textarea[name='extraInfo']").val("");
                form.render();
            });
        	
            form.on('select(projectNameSelect)', function(data){ //治疗项目下拉栏监听事件
            	 var type = $("#projectNameSelect option:selected").attr("type");
                 var typeInt = $("#projectNameSelect option:selected").attr("typeInt");
                 $("#type").val(type);
                 $("#type").attr("typeInt",typeInt);
			});
            
            $('input[name="cost"]').change(function () {  
            	//当治疗次数发生改变时,费用金额为次数乘以价格
            	var numberValue = $('input[name="cost"]').val(); //次数的值
            	numberValue = vertifyNumber(numberValue); //如果有非法字符返回0;
            	$('input[name="cost"]').val(numberValue); 
            });
        }
    });
    
    function formData(){        //获取表单数据
        var projectId=$("select[name='projectName']").find("option:selected").val();
        var projectName=$("select[name='projectName']").find("option:selected").text();
        var cost=$("input[name='cost']").val();
        var extraInfo=$("textarea[name='extraInfo']").val();
        var deadline = $("#deadline").val();
        var data={
            projectId:projectId,
            projectName:projectName,
            type :$("input[name='type']").val(),
            typeInt : $("input[name='type']").attr("typeInt"),
            cost:cost,
            deadline: deadline,
            extraInfo:extraInfo
        };
        return data;
    }
    
    function importFundSubmit(){      //父框点击新增框的新增按钮调用此函数
        $('#submit').click();
        var is=isSuccess;
        isSuccess=0;
        return is;
    }
    
    function vertifyNumber(x) {  //验证次数输入值合法性
    	var f1 = parseFloat(x); //判断"a11"类的错误
		if (isNaN(f1)) {
			return 0;
		}
		var f2 = Math.round(x*100)/100; //判断"11a"类的错误 
		if (isNaN(f2)) {
			return 0;
		}
		
		if(f2 < 0) {
			return 0;
		}
    	return f2;
    }
</script>
</html>
